
export class DateUtil {

    public static formatNumStr(num: number) {
        let str = "" + num;
        if (num < 10) {
            str = "0" + num;
        }
        return str;
    }

    public static formateYearMonthDayStr(timestamp: number) {
        let date: Date = new Date(timestamp);
        return date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
    }
    public static formateNumYearMonthDayStr(timestamp: number) {
        let date: Date = new Date(timestamp);
        return date.getFullYear() + "年" + (date.getMonth() + 1) + "月" + date.getDate() + "日";
    }

    public static formateMonthDayStr(timestamp: number) {
        let date: Date = new Date(timestamp);
        return (date.getMonth() + 1) + "月" + date.getDate() + "日";
    }

    //  timestamp:1453094034000  2018-1-31 19:53:44
    //根据时间戳返回 2018-1-31 19:53:44 
    public static formatDateStr(timestamp: number) {
        let date: Date = new Date(timestamp);
        return date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + " " + this.formatNumStr(date.getHours()) + ":" + this.formatNumStr(date.getMinutes()) + ":" + this.formatNumStr(date.getSeconds());
    }

    //  timestamp:1453094034000  2018-1-31-19-53-44 
    //根据时间戳返回 2018-1-31-19-53-44 
    public static formatDateStr2(timestamp: number) {
        let date: Date = new Date(timestamp);
        return date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + "-" + this.formatNumStr(date.getHours()) + "-" + this.formatNumStr(date.getMinutes()) + "-" + this.formatNumStr(date.getSeconds());
    }

    //  timestamp:1453094034000  2018-1-31
    //根据时间戳返回 2018-1-31
    public static formatDateStr3(timestamp: number) {
        let date: Date = new Date(timestamp);
        return date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
    }

    //  timestamp:1453094034000  2018-1-31
    //根据时间戳返回 2018年11月10日
    public static formatDateStr4(timestamp: number) {
        let date: Date = new Date(timestamp);
        return date.getFullYear() + "年" + (date.getMonth() + 1) + "月" + date.getDate() + "日";
    }

    //  timestamp:1453094034000  2018-1-31
    //根据时间戳返回 11月10日
    public static formatDateStr5(timestamp: number) {
        let date: Date = new Date(timestamp);
        return (date.getMonth() + 1) + "月" + date.getDate() + "日";
    }
    //  timestamp:1453094034000  2018-1-31
    //根据时间戳返回 11月10日9点
    public static formatDateStr6(timestamp: number) {
        let date: Date = new Date(timestamp);
        return (date.getMonth() + 1) + "月" + date.getDate() + "日" + this.formatNumStr(date.getHours()) + "点";
    }

    //  timestamp:1453094034000  
    //根据时间戳返回 19:53
    public static formatHourMinStr(timestamp: number) {
        let date: Date = new Date(timestamp);
        return this.formatNumStr(date.getHours()) + ":" + this.formatNumStr(date.getMinutes());
    }

    //  timestamp:1453094034000  
    //根据时间戳返回 19:53:11
    public static formatHourMinSecondStr(timestamp: number) {
        let date: Date = new Date(timestamp);
        return this.formatNumStr(date.getHours()) + ":" + this.formatNumStr(date.getMinutes()) + ":" + this.formatNumStr(date.getSeconds());
    }

    public static now(): number {
        let date: Date = new Date();
        return date.getTime();
    }

    public static betweenTime(startTime: number, endTime: number) {
        let date: Date = new Date();
        if (date.getTime() >= startTime && date.getTime() <= endTime) {
            return true;
        }
        return false;
    }

    //根据时间戳返回 19:53:11
    public static formatLeftTime0(timestamp: number) {
        let result: string = "";
        let hour: number = Math.floor(timestamp / (1000 * 60 * 60)) % 24;
        let min: number = Math.floor(timestamp / (1000 * 60)) % 60;
        let second: number = Math.floor(timestamp / 1000) % 60;
        if (hour > 0) {
            result = this.formatNumStr(hour) + ":" + this.formatNumStr(min) + ":" + this.formatNumStr(second);
        } else {
            result = this.formatNumStr(min) + ":" + this.formatNumStr(second);
        }
        return result
    }

    //根据时间戳返回 19:53:11
    public static formatLeftTime2(timestamp: number) {
        let result: string = "";
        let hour: number = Math.floor(timestamp / (1000 * 60 * 60)) % 24
        let min: number = Math.floor(timestamp / (1000 * 60)) % 60
        let second: number = Math.floor(timestamp / 1000) % 60
        result = this.formatNumStr(hour) + ":" + this.formatNumStr(min) + ":" + this.formatNumStr(second)
        return result
    }

    //根据时间戳返回 1天19:53:11
    public static formatLeftTime(timestamp: number) {
        let result: string = "";
        let day: number = Math.floor(timestamp / (1000 * 60 * 60 * 24))
        let hour: number = Math.floor(timestamp / (1000 * 60 * 60)) % 24
        let min: number = Math.floor(timestamp / (1000 * 60)) % 60
        let second: number = Math.floor(timestamp / 1000) % 60
        result = day + "天" + this.formatNumStr(hour) + ":" + this.formatNumStr(min) + ":" + this.formatNumStr(second)
        return result
    }

    //根据时间戳返回 1天13小时02分钟
    public static formatLeftTime3(timestamp: number) {
        let result: string = "";
        let day: number = Math.floor(timestamp / (1000 * 60 * 60 * 24))
        let hour: number = Math.floor(timestamp / (1000 * 60 * 60)) % 24
        let min: number = Math.floor(timestamp / (1000 * 60)) % 60
        let second: number = Math.floor(timestamp / 1000) % 60
        if (day > 0) {
            result = day + "天" + this.formatNumStr(hour) + "小时" + this.formatNumStr(min) + "分钟";
        } else {
            result = this.formatNumStr(hour) + "小时" + this.formatNumStr(min) + "分钟";
        }
        return result
    }

    //根据时间戳返回 1天13小时02分钟
    public static formatLeftTime4(timestamp: number) {
        let result: string = "";
        let day: number = Math.floor(timestamp / (1000 * 60 * 60 * 24))
        let hour: number = Math.floor(timestamp / (1000 * 60 * 60)) % 24
        let min: number = Math.floor(timestamp / (1000 * 60)) % 60
        let second: number = Math.floor(timestamp / 1000) % 60
        if (day > 0) {
            result = day + "天" + this.formatNumStr(hour) + "小时" + this.formatNumStr(min) + "分钟";
        } else {
            result = this.formatNumStr(hour) + "小时" + this.formatNumStr(min) + "分钟";
        }
        return result
    }

    // 根据秒数返回 00:00
    public static formatLeftTime5(time: number) {
        let min: number = Math.floor(time / 60);
        let second: number = time % 60;
        let minStr: string = min >= 10 ? min.toString() : ("0" + min);
        let secondStr: string = second >= 10 ? second.toString() : ("0" + second);
        let str = minStr + ":" + secondStr;
        return str;
    }

    // 根据秒数返回 00分00秒
    public static formatLeftTime6(time: number) {
        let min: number = Math.floor(time / 60);
        let second: number = time % 60;
        let minStr: string = min >= 10 ? min.toString() : ("0" + min);
        let secondStr: string = second >= 10 ? second.toString() : ("0" + second);
        let str = minStr + "分" + secondStr + "秒";
        return str;
    }

    public static isToday(dateTime: number): boolean {
        let nowDate: Date = new Date();
        let checkDate: Date = new Date(dateTime);
        if (checkDate.getFullYear() == nowDate.getFullYear() && checkDate.getMonth() == nowDate.getMonth() && checkDate.getDate() == nowDate.getDate()) {
            return true;
        }
        return false;
    }

    //2020-04-29 10:00:00 根据时间string返回date
    public static getDateByStr(dateStr: string) {
        let arr: Array<string> = dateStr.split(" ");
        let arr2: Array<string> = arr[0].split("-");
        let arr3: Array<string> = arr[1].split(":");
        let year: number = parseInt(arr2[0]);
        let month: number = parseInt(arr2[1]) - 1;
        let day: number = parseInt(arr2[2]);
        let hour: number = parseInt(arr3[0]);
        let min: number = parseInt(arr3[1]);
        let second: number = parseInt(arr3[2]);
        let date: Date = new Date(year, month, day, hour, min, second);
        // Logger.log("getDateByStr==", dateStr, arr, arr2, arr3, year, month, day, hour, min, second, date);
        return date;

    }

    //返回过去几天
    public static getPassDay(checkTime: number) {
        let now: number = DateUtil.now();
        let checkDate: Date = new Date(checkTime);
        checkDate = new Date(checkDate.getFullYear(), checkDate.getMonth(), checkDate.getDate());
        let passTime: number = now - checkDate.getTime();
        if (passTime < 0) {
            passTime = 0;
        }
        let day: number = Math.floor(passTime / (1000 * 60 * 60 * 24))
        return day;
    }


    public static getDay(checkTime: number) {
        let day: number = Math.floor(checkTime / (1000 * 60 * 60 * 24))
        return day;
    }
    /**根据秒 返回  时  分 秒 */
    public static fomatTimeBySecond(second: number, showSecond: boolean = true) {
        //计算分钟
        //算法：将秒数除以60，然后下舍入，既得到分钟数
        let d = Math.floor((second / 86400));
        let h = Math.floor((second % 86400) / 3600);
        let m = Math.floor(((second % 86400) % 3600) / 60);
        let s = Math.floor(((second % 86400) % 3600) % 60);

        let str = '';
        str += d > 0 ? d + '天' : '';
        str += h > 0 ? h + '时' : '';
        str += m > 0 ? m + '分钟' : "";
        if (showSecond) {
            str += s > 0 ? s + '秒' : "";
        }
        return str;
    }


    /**
     * @desc 返回指定日期的星期一的Date对象
     * @return Date: 返回指定日期的星期一的日期对象
     * @param d 指定日期
     */
    public static getWeekByDate(nowDate: Date): Date {
        let leftDate: number = nowDate.getDay();
        let resultDate: Date = new Date(nowDate.getFullYear(), nowDate.getMonth(), nowDate.getDate() - leftDate)
        return resultDate
    }


    public static getMonthByDate(nowDate: Date) {
        let resultDate: Date = new Date(nowDate.getFullYear(), nowDate.getMonth(), 1)
        return resultDate
    }
}
